Reducing shader switches

Kanzi must switch shaders every time when rendered batch has a different shader compared to the previous batch. Because materials can have their own vertex and fragment shaders, in some cases your application has to switch shaders several times during the rendering of a scene.

In OpenGL vertex and fragment shaders are each compiled into a shader program and are sent to the GPU whenever it needs to be used. The GPU only stores one program at a time. At least one shader switch must be made for each material in a scene. If objects are presented in an order that requires excessive switching between shader programs, the rendering can slow down.

Shader switching decreases the performance of your application, so keep shader switching to a minimum. To reduce shader switches:

Rendering objects by material type

Even though you can increase performance by rendering objects by material type, sorting batches by shader can be much more efficient, because sorting by batches takes the sub-materials into account. See Sorting batches by shader in render passes.

Note This operation increases the use of CPU.

To render objects by the material type:

  1. In the Library right-click Composing and select Create > Sorting Filter.
  2. In the Properties set property Sorting Type to Material type.
  3. In the Library in Composing > Composers select the composer you are using, and there select the render pass you are using.
  4. In the render pass set the Object Source property to the sorting filter you created in the first step.

Sorting batches by shader in render passes

Note This operation increases the use of CPU.

To sort batches by shader in render passes:

  1. In the Library select Composing > Composers and in the composer select the render pass you want to set.
  2. In the Properties enable the Sort Batches by Shader property.

See also

Using binary shaders

Optimizing pixel shaders

Loading resources in parallel

Shaders best practices

Measuring the performance of your Kanzi application

Best practices